%% -*- erlang -*-

IsWindows = case os:type() of {win32, _} -> true; {_, _} -> false end,

FilterRocksDbCheck = (IsWindows orelse os:getenv("AE_DISABLE_ROCKSDB") =/= false),

FilterRelxRocksDb = fun(Apps) ->
                        case FilterRocksDbCheck of
                            false ->
                                Apps;
                            true ->
                                Apps -- [rocksdb, mnesia_rocksdb, {rocksdb, load}, {mnesia_rocksdb, load}]
                        end
                    end,

FilterDepsRocksDb = fun(Deps) ->
                        case FilterRocksDbCheck of
                            false ->
                                Deps;
                            true ->
                                lists:foldl(
                                    fun(App, Acc) ->
                                        lists:keydelete(App, 1, Acc)
                                    end,
                                    Deps,
                                    [rocksdb, mnesia_rocksdb]
                                )
                        end
                    end,

%% In order to avoid ugly dynamic module loading over rpc - inject meck into the common test release
%% This does not inject any applications into the system tests, smoke tests and production releases
Args = init:get_plain_arguments(),
IsCommonTestRelease = lists:member("test", Args) and lists:member("release", Args),
AddMeckInCommonTests = fun(Apps) ->
                          case IsCommonTestRelease of
                              false ->
                                  Apps;
                              true ->
                                  [meck] ++ Apps
                          end
                       end,

% We check all overlay entries' destinations against these regular expressions,
% and replacement found matches with the corresponding .cmd entry.
OverlayReplacements = [
                       "^bin/extensions/.*"
                      ],

AdaptOverlayBin = fun(Overlay) ->
                      case IsWindows of
                          false ->
                              Overlay;
                          true ->
                              lists:foldl(
                                fun
                                    ({copy, Src0, Dest0}, Acc0) ->
                                        lists:foldl(
                                          fun(Format, Acc1) ->
                                              {ok, Re} = re:compile(Format),
                                              case re:run(Dest0, Re) of
                                                  nomatch ->
                                                      Acc1;
                                                  _ ->
                                                      Src = filename:rootname(Src0) ++ ".cmd",
                                                      Dest = filename:rootname(Dest0) ++ ".cmd",
                                                      lists:keyreplace(Dest0, 3, Acc1, {copy, Src, Dest})
                                              end
                                          end, Acc0, OverlayReplacements);
                                    (_, Acc0) ->
                                        Acc0
                                end, Overlay, Overlay)
                      end
                  end,

%% Update relx config
Relx0 = lists:keyfind(relx, 1, CONFIG),
{relx, [{release, {aeternity, _}, RelxApps0} | RelxT0]} = Relx0,
{overlay, RelxOverlay0} = lists:keyfind(overlay, 1, RelxT0),
RelxT1 = lists:keyreplace(overlay, 1, RelxT0, {overlay, AdaptOverlayBin(RelxOverlay0)}),
{ok, VersionBin} = file:read_file(<<"VERSION">>),
Version = string:trim(binary_to_list(VersionBin)),
%% the release should be in front
RelxApps1 = FilterRelxRocksDb(RelxApps0),
RelxApps2 = AddMeckInCommonTests(RelxApps1),
Relx = {relx, [{release, {aeternity, Version}, RelxApps2}] ++ RelxT1},
CONFIG1 = lists:keyreplace(relx, 1, CONFIG, Relx),

%% Update rebar deps
{deps, Deps0} = lists:keyfind(deps, 1, CONFIG1),
lists:keyreplace(deps, 1, CONFIG1, {deps, FilterDepsRocksDb(Deps0)}).
